<?php
/**
 * Created by PhpStorm.
 * User: 王凯林
 * Date: 18/3/25
 * Time: 18:10
 */

namespace App\Http\Controllers\H5\Shop;


use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Http\Controllers\Controller;
use App\Model\MoneyReport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CashRecordController extends Controller
{
    /*
     * 个人中心申请提现的接口
     */
    public function cashStore(Request $request){
        if($request->has('uid')&&$request->has('amount')&&$request->has('account_name')&&$request->has('account')){
            //采用事务的方式
            DB::beginTransaction();
            try{
                //提现金额不能小于100元
                if($request->amount < 100.00){
                    return ajaxError('提现金额小于100元，操作失败！');
                    die;
                }
                //申请提现之后将提现的这部分金额status和types>>>money_report
                $data1 = [
                    'types'=>5,
                    'status'=>2,
                    'uid'=>$request->uid,
                    'amount'=>'-'.$request->amount,
                    'create_time'=>time(),
                    'remark'=>'提现中'
                ];
                $re1 = MoneyReport::create($data1);
                //操作数据往cash_records写入，初始操作即为：1（待处理），等待客服后台处理
                $data2 = [
                    'status'=>1,
                    'uid'=>$request->uid,
                    'create_time'=>time(),
                    'amount'=>$request->amount,
                    'account'=>$request->account,
                    'account_name'=>$request->account_name
                ];
                $re2 = \App\Model\CashRecord::create($data2);
                if($re1 && $re2){
                    DB::commit();
                    return ajaxSuccess();
                }
            }catch(\Exception $e){
                DB::rollBack();
                return ajaxError();
            }

        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
    }


}